import router from "@/router";
import { $alert, $singleMsg } from "@/secondary/globalProperties/message";
import { getCookie } from "@/secondary/utils/cache";
import { PlainObjType } from "@/typings";
import { AxiosHeaders, AxiosResponse } from "axios";

/**
 * axios 交互
 */
const httpInteracte = {
  // 登录失效
  LOGIN_FAILURE: async () => {
    await $alert({
      tip: `登录失效，error code: ${status}`,
      title: "提示",
      type: "warning",
      callback: () => {
        router.push({
          path: "/logout",
          query: {
            redirect: `${window.encodeURIComponent(window.location.href)}`,
          },
        });
      },
    });
  },

  // 接口报错
  API_ERROR(res: AxiosResponse) {
    $singleMsg(res.data.msg || "接口报错");
  },

  // 超时
  API_TIMEOUT() {
    $singleMsg("响应超时！");
  },

  // 接口无权限
  API_AUTH(status: number) {
    $singleMsg(`请求接口无权限! code: ${status}`);
  },

  // 服务器异常
  API_SERVER_ERROR(status: number) {
    $singleMsg(`服务器异常，error code: ${status}`);
  },

  // 设置 header 的 authorization
  SET_HEADER_AUTH(headers: AxiosHeaders | PlainObjType = {}) {
    headers.authorization = getCookie("token");
  },
};

export default httpInteracte;
